﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CodeBrigadeTE
{
    public class Utils
    {
        static public string GetUserDataCacheFileName()
        {
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            dir = Path.Combine(dir, "CodeBrigadeTE");
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            string file = Path.Combine(dir, "cache.dat");
            return file;
        }

        public static string GetSpreadsheetColumnName(int columnNumber) // ?
        {
            int dividend = columnNumber;
            string columnName = String.Empty;
            int modulo;

            while (dividend > 0)
            {
                modulo = (dividend - 1) % 26;
                columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
                dividend = (int)((dividend - modulo) / 26);
            }

            return columnName;
        }

        // TODO: search for analog class in google api
        public struct CellCoordinates // ?
        {
            public uint column;
            public uint row;

            public CellCoordinates(uint column, uint row)
            {
                this.column = column;
                this.row = row;
            }

            public CellCoordinates(string codeBlockName)
            {
                string columnPart = codeBlockName.Substring(codeBlockName.IndexOf('C')+1);
                string rowPart = codeBlockName.Substring(codeBlockName.IndexOf('R')+1, codeBlockName.IndexOf('C')-1);
                this.column = Convert.ToUInt32(columnPart);
                this.row = Convert.ToUInt32(rowPart);
            }

            public CellCoordinates(Google.GData.Spreadsheets.CellEntry entry)
            {
                this.column = entry.Column;
                this.row = entry.Row;
            }

            public string GetHash()
            {
                // Google standard hash format
                return "R" + row.ToString() + "C" + column.ToString();
            }
        }
    }
}
